create TRIGGER Candidates_ITrig ON Candidates AFTER Insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.Id
end
go

create TRIGGER Candidates_UTrig ON Candidates AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 
  
  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.Id
  join deleted d on d.Id = i.Id
  where (isnull(i.ApproveOnPost,-1) <> isnull(d.ApproveOnPost,-1) or
		 isnull(i.ManagerId,0) <> isnull(d.ManagerId,0) or
		 isnull(i.SexID,0) <> isnull(d.SexID,0) or
		 isnull(i.CarID,0) <> isnull(d.CarID,0) or
		 isnull(i.SalaryRangeID,0) <> isnull(d.SalaryRangeID,0) or
		 isnull(i.Position_CompareImportantID,0) <> isnull(d.Position_CompareImportantID,0) or
		 isnull(i.Territory_CompareImportantID,0) <> isnull(d.Territory_CompareImportantID,0) or
		 isnull(i.SalaryRange_CompareImportantID,0) <> isnull(d.SalaryRange_CompareImportantID,0) or
		 isnull(i.TerritoryID,0) <> isnull(d.TerritoryID,0) or
		 isnull(i.IsCandidate,-1) <> isnull(d.IsCandidate,-1) or
		 isnull(i.IsContactPerson,-1) <> isnull(d.IsContactPerson,-1) or
	     isnull(i.DocumentSum,0) <> isnull(d.DocumentSum,0) or
		 isnull(d.DateOfBirthDay,'1 jan 2000') <> isnull(i.DateOfBirthDay,'1 jan 2000') or
		 isnull(d.EnterDate,'1 jan 2000') <> isnull(i.EnterDate,'1 jan 2000') or
	     isnull(i.DocumnetNumber,'') <> isnull(d.DocumnetNumber,'') or
		 isnull(i.LastName,'') <> isnull(d.LastName,'') or
		 isnull(i.Adress,'') <> isnull(d.Adress,'') or
		 isnull(i.PrePaid,'') <> isnull(d.PrePaid,'') or
		 isnull(i.Education,'') <> isnull(d.Education,'') or
		 isnull(i.ResumeName,'') <> isnull(d.ResumeName,'') or
		 isnull(i.EducationType,'') <> isnull(d.EducationType,'') or
		 isnull(i.OnlyTradeMarks,'') <> isnull(d.OnlyTradeMarks,'') or
		 update(Resume) or
		 update(Characteristic) or
		 update(OtherReqirments)) and
		isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))

end
go

------

create TRIGGER Candidat_EMails_UTrig ON Candidat_EMails AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  join deleted d on d.Id = i.Id
  where isnull(d.EMail,'') <> isnull(i.EMail,'') and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_EMails_ITrig ON Candidat_EMails AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_EMails_DTrig ON Candidat_EMails AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidatId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidat_ExceptCommodityGroups_UTrig ON Candidat_ExceptCommodityGroups AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  join deleted d on d.Id = i.Id
  where isnull(d.CommodityGroupId,0) <> isnull(i.CommodityGroupId,0) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_ExceptCommodityGroups_ITrig ON Candidat_ExceptCommodityGroups AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_ExceptCommodityGroups_DTrig ON Candidat_ExceptCommodityGroups AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidatId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidat_ExceptEmployers_UTrig ON Candidat_ExceptEmployers AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  join deleted d on d.Id = i.Id
  where isnull(d.EmployerID,0) <> isnull(i.EmployerID,0) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_ExceptEmployers_ITrig ON Candidat_ExceptEmployers AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_ExceptEmployers_DTrig ON Candidat_ExceptEmployers AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidatId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidat_Expiriences_UTrig ON Candidat_Expiriences AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  join deleted d on d.Id = i.Id
  where (isnull(d.PositionID,0) <> isnull(i.PositionID,0) or
		 isnull(d.Years,0) <> isnull(i.Years,0)) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_Expiriences_ITrig ON Candidat_Expiriences AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_Expiriences_DTrig ON Candidat_Expiriences AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidatId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidat_Phones_UTrig ON Candidat_Phones AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  join deleted d on d.Id = i.Id
  where (isnull(d.Phone,'') <> isnull(i.Phone,'') or
		 update(comment)) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_Phones_ITrig ON Candidat_Phones AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidatID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_Phones_DTrig ON Candidat_Phones AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidatId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidat_RangeTerritories_UTrig ON Candidat_RangeTerritories AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidateID
  join deleted d on d.Id = i.Id
  where isnull(d.RangeTerritoryId,0) <> isnull(i.RangeTerritoryId,0) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_RangeTerritories_ITrig ON Candidat_RangeTerritories AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidateID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidat_RangeTerritories_DTrig ON Candidat_RangeTerritories AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidateId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

------

create TRIGGER Candidate_Positions_UTrig ON Candidate_Positions AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidateID
  join deleted d on d.Id = i.Id
  where isnull(d.PositionId,0) <> isnull(i.PositionId,0) and
        isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidate_Positions_ITrig ON Candidate_Positions AFTER insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join inserted i on c.Id = i.CandidateID
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER Candidate_Positions_DTrig ON Candidate_Positions AFTER Delete
AS 
BEGIN
  if @@rowcount = 0 return	 

  update c set
    c.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Candidates c
  join deleted d on d.CandidateId = c.Id
  where isnull(c.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO